package bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {

    public static void main(String[] args) throws Exception {

        ExecutorService executorServicePool= Executors.newCachedThreadPool();
        ServerSocket serverSocket=new ServerSocket(8080);
        while (true){
          final Socket socket= serverSocket.accept();
          executorServicePool.execute(()->{
              System.out.println("当前线程name"+Thread.currentThread().getName());
                handler(socket);
          });
        }
    }

    public static void handler(Socket socket)  {
        System.out.println("当前线程name"+Thread.currentThread().getName());

        try {
            byte []bytes=new byte[1024];
            InputStream ins=socket.getInputStream();
            while (true){
                int read=ins.read(bytes);
                if(read!=-1){
                    System.out.println(new String(bytes,0,read));
                }else {
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
